using ZhiHeng.Consume.DataContract;

namespace ZhiHeng.Consume.ServiceLib.DAL
{
    public abstract class BaseRepository
    {
        //Our entity framework engine used in the solution
        internal OneCardParkEntities _context = DBContextFactory.Instance.GetDB();
        //Switch that tells us if the datacontext is reused
        internal bool _contextReused;

        //This return the current, or a new connection through the EF
        public OneCardParkEntities GetObjectContext()
        {
            if (!_contextReused)
            {
                return DBContextFactory.Instance.GetDB();
            }
            return _context;
        }

        //This is the public method that we will call from our repository
        public void ReleaseObjectContextIfNotReused()
        {
            if (!_contextReused)
            {
                ReleaseObjectContext();
            }
        }

        //Simple dispose of the current EF
        public void ReleaseObjectContext()
        {
            if (_context != null)
            {
                _context.Dispose();
            }
            _contextReused = false;
        }

    }
}